﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PageTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {

        private int pageSize = 10;

        public int PageSize
        {
            get { return pageSize; }
            set
            {
                this.pageSize = value;
                RaisePropertyChanged(nameof(PageSize));
            }
        }


        private int pageCount;

        public int PageCount
        {
            get { return pageCount; }
            set
            {
                pageCount = value;
                RaisePropertyChanged("PageCount");
            }
        }



        private int currentPage;

        public int CurrentPage
        {
            get { return currentPage; }
            set
            {
                currentPage = value;
                RaisePropertyChanged("CurrentPage");
            }
        }



        private ObservableCollection<BuzModel> datas;

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public ObservableCollection<BuzModel> TotalDatas
        {
            get { return datas; }
            set { datas = value; }
        }

        private ObservableCollection<BuzModel> filteredDatas;

        public ObservableCollection<BuzModel> FilteredDatas
        {
            get { return filteredDatas; }
            set { filteredDatas = value;}
        }



        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            TotalDatas = new ObservableCollection<BuzModel>();
            FilteredDatas = new ObservableCollection<BuzModel>();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            GenFu.GenFu.Configure<BuzModel>().Fill(p => p.Tel).WithinRange(8000000,8999999);
            var fakeDatas = GenFu.GenFu.ListOf<BuzModel>(100);
            fakeDatas.ForEach((i)=>
            {
                TotalDatas.Add(i);
            });
            PageTuring(1);
        }

        #region 命令
        private RelayCommand<int> onPageSizeChangeCmd;
        /// <summary>
        /// 当页行数变化，翻页到第一页
        /// </summary>
        public RelayCommand<int> OnPageSizeChangeCmd
        {
            get
            {
                return onPageSizeChangeCmd ?? (new RelayCommand<int>((p) => 
                {
                    CalcPageSize();
                    PageTuring(1);
                    CurrentPage = 1;

                }));
            }
        }



        private RelayCommand<int> firstPageCmd;

        public RelayCommand<int> FirstPageCmd
        {
            get
            {
                return firstPageCmd ?? (new RelayCommand<int>((p) =>
                {
                    PageTuring(1);
                    CurrentPage = 1;
                }));
            }
        }

        private RelayCommand<int> nextPageCmd;

        public RelayCommand<int> NextPageCmd
        {
            get
            {
                return nextPageCmd ?? (new RelayCommand<int>((p)=>
                {
                    CalcPageSize();
                    if (CurrentPage < PageCount)
                    {
                        CurrentPage++;
                        PageTuring(CurrentPage);
                    }
                }));
            }
        }

        private RelayCommand<int> prePageCmd;

        public RelayCommand<int> PrePageCmd
        {
            get
            {
                return prePageCmd ?? (new RelayCommand<int>((p)=>
                {
                    CalcPageSize();
                    if (CurrentPage > 1)
                    {
                        CurrentPage--;
                        PageTuring(CurrentPage);
                    }
                }));
            }
        }

        private RelayCommand<int> lastPageCmd;

        public RelayCommand<int> LastPageCmd    
        {
           get
          {
                return lastPageCmd ?? (new RelayCommand<int>((p)=>
                {
                    CalcPageSize();
                    PageTuring(PageCount);
                }));
          }
        }

        private RelayCommand<int> goToCmd;

        public RelayCommand<int> GoToCmd
        {
           get
           {
               return goToCmd ?? (new RelayCommand<int>((p)=>
               {
                   CalcPageSize();
                   if (p > 0 && p <= PageCount)
                   {
                       CurrentPage = p;
                       PageTuring(CurrentPage);
                   }
               }));
           }
        }


        #endregion

        /// <summary>
        /// 翻页（UI页数 => 集合序号 + 1）
        /// </summary>
        /// <param name="pageNum"></param>
        public void PageTuring(int pageNum)
        {
            if (PageCount == 0) CalcPageSize();
            if (pageNum > 0 && pageNum <= PageCount)
            {
                CurrentPage = pageNum;
                var temp = CurrentPage;
                var filterDatas = TotalDatas.Skip((temp - 1) * PageSize).Take(PageSize).ToList();
                FilteredDatas.Clear();
                ListToObservableCollection<BuzModel>(filterDatas, ref filteredDatas);
            }
        }

        public void ListToObservableCollection<T>(IEnumerable<T> datas,ref ObservableCollection<T> result)
        {
            foreach (var item in datas)
            {
                result.Add(item);
            }
        }

        private void CalcPageSize()
        {
            if (TotalDatas.Count % PageSize == 0)
            {
                PageCount = TotalDatas.Count / PageSize;
            }
            else
            {
                PageCount = TotalDatas.Count / PageSize + 1;
            }
        }
    }
}
